<script setup>
import {cloneDeep} from "lodash-es";
import {arr2Tree, objMerge} from "@/utils/tools.js";
import SettingUserAPI from "@/api/setting/user.js";
import SettingRoleAPI from "@/api/setting/role.js";
import {ElMessageBox} from "element-plus";

const emits = defineEmits(["close"]);
const priId = ref(0)
const open = (id) => {
  if (id) {
    dialog.title = '编辑'
    priId.value = id
    SettingUserAPI.getFormData(id).then(res => {
      objMerge(formData, res)
    })
  } else {
    dialog.title = '新增'
    priId.value = 0
  }
  dialog.visible = true;
  SettingRoleAPI.getOptions().then(res => {
    roleList.value = res
  })
}
defineExpose({open});

const dialog = reactive({
  visible: false,
  title: "新增",
});

const formRef = ref();
let formData = reactive({
  nickname: "",
  account: "",
  roles: null,
});

const rules = reactive({
  nickname: [{required: true, message: "不能为空", trigger: "blur"}],
  account: [{required: true, message: "不能为空", trigger: "blur"}],
  roles: [{required: true, message: "请选择", trigger: "blur"}],
});


const __DialogClose = () => {
  dialog.visible = false;
  formRef.value.resetFields();
  formRef.value.clearValidate();
  emits("close");
};

const _submit = (formEl) => {
  if (!formEl) return
  const data = cloneDeep(formData)
  formEl.validate((valid, fields) => {
    if (valid) {
      data.roles = [data.roles]
      if (priId.value > 0) {
        SettingUserAPI.update(priId.value, data).then(res => {
          __DialogClose()
        })
      } else {
        SettingUserAPI.add(data).then(res => {
          ElMessageBox.alert(res.password, '请保存密码：')
          __DialogClose()
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

const roleList = ref([])

</script>

<template>
  <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      append-to-body
      destroy-on-close
      width="30vw"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="__DialogClose"
      draggable
  >
    <el-form ref="formRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="名称" prop="nickname">
        <el-input v-model="formData.nickname" maxlength="20" placeholder="名称"/>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="formData.account" maxlength="20" placeholder="账号"/>
      </el-form-item>
      <el-form-item label="角色" prop="roles">
        <el-select v-model="formData.roles" style="width: 240px">
          <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="__DialogClose">取 消</el-button>
        <el-button type="primary" @click="_submit(formRef)">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
